package com.ruantu.clothing.service.report;

import com.ruantu.clothing.dao.report.StoreReportMapper;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.report.StoreReport;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/10 0010.
 */
@Service
public class StoreReportServiceImpl implements StoreReportService {


    @Autowired
    private StoreReportMapper storeReportMapper;

    @Override
    public List<StoreReport> findStoreReport(StoreReport stortReport, int fenlei, String sort, String order, String jgid, PageBean pageBean) {
        Map<String, Object> map = new HashMap<>();
        List<StoreReport> store = new ArrayList<>();
        if (StringUtils.isNotBlank(stortReport.getProduct())) {
            map.put("product", stortReport.getProduct());
        }
        if (StringUtils.isNotBlank(stortReport.getPinpai())) {
            map.put("pinpai", stortReport.getPinpai());
        }
        if (StringUtils.isNotBlank(stortReport.getSeason())) {
            map.put("season", stortReport.getSeason());
        }
        if (StringUtils.isNotBlank(stortReport.getYear())) {
            map.put("year", stortReport.getYear());
        }
        map.put("fenlei", fenlei);
        map.put("jgid", jgid);
        map.put("sort", sort);
        map.put("order", order);
        map.put("start", pageBean.getStart());
        map.put("rows", pageBean.getRows());
        //查询款号
        List<String> product = storeReportMapper.findProduct(map);
        //查询总数量
        pageBean.setTotal(storeReportMapper.findStoreReportCount(map));
        if (product.size() == 0) {
            return store;
        } else {
            StringBuffer products = new StringBuffer();
            for (int i = 0; i < product.size(); i++) {
                if (i == product.size() - 1) {
                    products.append("'" + product.get(i) + "'");
                } else {
                    products.append("'" + product.get(i) + "',");
                }
            }
            map.put("products", products);
        }
        //查询库存
        store = storeReportMapper.findStoreReport(map);
        //查询裁剪数量
        List<StoreReport> tailor = new ArrayList<>();
        if ("1".equals(jgid)) {
            tailor = findTailorByProduct(map);
        }
        //查询入库数量
        List<StoreReport> ruku = findRukuByProducr(map);
        //查询出库数量
        List<StoreReport> chuku = findChukuByProduct(map);
        store = chuLi(store, tailor, ruku, chuku, fenlei);
        return store;
    }

    /**
     * 处理数据  复用代码
     *
     * @param store
     * @param tailor
     * @param ruku
     * @param chuku
     * @param fenlei
     * @return
     */
    public List<StoreReport> chuLi(List<StoreReport> store, List<StoreReport> tailor, List<StoreReport> ruku, List<StoreReport> chuku, int fenlei) {
        //按款号 fenlei=0按款号fenlei=1按颜色fenlei=2按尺码
        for (int i = 0; i < store.size(); i++) {
            //裁剪数量
            for (int j = 0; j < tailor.size(); j++) {
                if (fenlei == 0) {
                    if (store.get(i).getProduct().equals(tailor.get(j).getProduct())) {
                        store.get(i).setCaijianshu(tailor.get(j).getCaijianshu());
                        break;
                    }
                } else if (fenlei == 1) {
                    if (store.get(i).getProduct().equals(tailor.get(j).getProduct()) && store.get(i).getColor().equals(tailor.get(j).getColor())) {
                        store.get(i).setCaijianshu(tailor.get(j).getCaijianshu());
                        break;
                    }
                } else if (fenlei == 2) {
                    if (store.get(i).getProduct().equals(tailor.get(j).getProduct()) && store.get(i).getColor().equals(tailor.get(j).getColor())
                            && store.get(i).getSize().equals(tailor.get(j).getSize())) {
                        store.get(i).setCaijianshu(tailor.get(j).getCaijianshu());
                        break;
                    }
                }
            }
            //查询入库数量
            for (int j = 0; j < ruku.size(); j++) {
                if (fenlei == 0) {
                    if (store.get(i).getProduct().equals(ruku.get(j).getProduct())) {
                        store.get(i).setRukushu(ruku.get(j).getRukushu());
                        break;
                    }
                } else if (fenlei == 1) {
                    if (store.get(i).getProduct().equals(ruku.get(j).getProduct()) && store.get(i).getColor().equals(ruku.get(j).getColor())) {
                        store.get(i).setRukushu(ruku.get(j).getRukushu());
                        break;
                    }
                } else if (fenlei == 2) {
                    if (store.get(i).getProduct().equals(ruku.get(j).getProduct()) && store.get(i).getColor().equals(ruku.get(j).getColor())
                            && store.get(i).getSize().equals(ruku.get(j).getSize())) {
                        store.get(i).setRukushu(ruku.get(j).getRukushu());
                        break;
                    }
                }
            }
            //查询出库数量
            for (int j = 0; j < chuku.size(); j++) {
                if (fenlei == 0) {
                    if (store.get(i).getProduct().equals(chuku.get(j).getProduct())) {
                        store.get(i).setChukushu(Math.abs(chuku.get(j).getChukushu()));
                        break;
                    }
                } else if (fenlei == 1) {
                    if (store.get(i).getProduct().equals(chuku.get(j).getProduct()) && store.get(i).getColor().equals(chuku.get(j).getColor())) {
                        store.get(i).setChukushu(Math.abs(chuku.get(j).getChukushu()));
                        break;
                    }
                } else if (fenlei == 2) {
                    if (store.get(i).getProduct().equals(chuku.get(j).getProduct()) && store.get(i).getColor().equals(chuku.get(j).getColor())
                            && store.get(i).getSize().equals(chuku.get(j).getSize())) {
                        store.get(i).setChukushu(Math.abs(chuku.get(j).getChukushu()));
                        break;
                    }
                }
            }
        }
        return store;
    }

    @Override
    public List<StoreReport> findTailorByProduct(Map<String, Object> map) {
        return storeReportMapper.findTailorByProduct(map);
    }

    @Override
    public List<StoreReport> findRukuByProducr(Map<String, Object> map) {
        return storeReportMapper.findRukuByProducr(map);
    }

    @Override
    public List<StoreReport> findChukuByProduct(Map<String, Object> map) {
        return storeReportMapper.findChukuByProduct(map);
    }

    @Override
    public List<StoreReport> findStoreReportFooter(String jgid, StoreReport stortReport) {
        Map<String, Object> map = new HashMap<>();
        map.put("jgid", jgid);
        if (StringUtils.isNotBlank(stortReport.getProduct())) {
            map.put("product", stortReport.getProduct());
        }
        if (StringUtils.isNotBlank(stortReport.getPinpai())) {
            map.put("pinpai", stortReport.getPinpai());
        }
        if (StringUtils.isNotBlank(stortReport.getSeason())) {
            map.put("season", stortReport.getSeason());
        }
        if (StringUtils.isNotBlank(stortReport.getYear())) {
            map.put("year", stortReport.getYear());
        }
        int shuliang = storeReportMapper.findStoreReportFooter(map);
        List<StoreReport> footer = new ArrayList<>();
        stortReport.setProduct("总计：");
        stortReport.setShuliang(shuliang);
        footer.add(stortReport);
        return footer;
    }


    @Override
    public XSSFWorkbook downloadExcel(StoreReport stortReport, String jgid, int fenlei) {
        Map<String, Object> map = new HashMap<>();
        List<StoreReport> store = new ArrayList<>();
        //查询裁剪数量
        List<StoreReport> tailor = new ArrayList<>();
        if (StringUtils.isNotBlank(stortReport.getProduct())) {
            map.put("product", stortReport.getProduct());
        }
        if (StringUtils.isNotBlank(stortReport.getPinpai())) {
            map.put("pinpai", stortReport.getPinpai());
        }
        if (StringUtils.isNotBlank(stortReport.getSeason())) {
            map.put("season", stortReport.getSeason());
        }
        if (StringUtils.isNotBlank(stortReport.getYear())) {
            map.put("year", stortReport.getYear());
        }
        map.put("fenlei", fenlei);
        map.put("jgid", jgid);
        map.put("sort", "shuliang");
        map.put("order", "desc");
        //查询款号
        List<String> product = storeReportMapper.findProduct(map);
        if (product.size() != 0) {
            StringBuffer products = new StringBuffer();
            for (int i = 0; i < product.size(); i++) {
                if (i == product.size() - 1) {
                    products.append("'" + product.get(i) + "'");
                } else {
                    products.append("'" + product.get(i) + "',");
                }
            }
            map.put("products", products);
            //查询库存
            store = storeReportMapper.findStoreReport(map);
            if ("1".equals(jgid)) {
                tailor = findTailorByProduct(map);
            }
            //查询入库数量
            List<StoreReport> ruku = findRukuByProducr(map);
            //查询出库数量
            List<StoreReport> chuku = findChukuByProduct(map);
            //需要导出的数据
            store = chuLi(store, tailor, ruku, chuku, fenlei);
        }
        //创建一个表格
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFont(font);
        CellStyle center = wb.createCellStyle();
        center.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("商品款号");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(1);
        cell.setCellValue("商品名称");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(2);
        cell.setCellValue("铭牌价");
        cell.setCellStyle(titleStyle);
        int count = 2;
        if (fenlei == 1 || fenlei == 2) {
            cell = row.createCell(++count);
            cell.setCellValue("颜色");
            cell.setCellStyle(titleStyle);
        }
        if (fenlei == 2) {
            cell = row.createCell(++count);
            cell.setCellValue("尺码");
            cell.setCellStyle(titleStyle);
        }
        if ("1".equals(jgid)) {
            cell = row.createCell(++count);
            cell.setCellValue("总裁剪");
            cell.setCellStyle(titleStyle);
        }
        cell = row.createCell(++count);
        cell.setCellValue("已入库");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("已出库");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("现有库存");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("品牌");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("季节");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("年份");
        cell.setCellStyle(titleStyle);

        for (int i = 0; i < store.size(); i++) {
            row = sheet.createRow(i + 1);
            //款号
            cell = row.createCell(0);
            cell.setCellValue(store.get(i).getProduct());
            cell.setCellStyle(center);
            //商品名
            cell = row.createCell(1);
            cell.setCellValue(store.get(i).getProductName());
            cell.setCellStyle(center);
            //铭牌价
            cell = row.createCell(2);
            cell.setCellValue(store.get(i).getPrice());
            count = 2;
            if (fenlei == 1 || fenlei == 2) {
                cell = row.createCell(++count);
                cell.setCellValue(store.get(i).getColor());
                cell.setCellStyle(center);
            }
            if (fenlei == 2) {
                cell = row.createCell(++count);
                cell.setCellValue(store.get(i).getSize());
                cell.setCellStyle(center);
            }
            if ("1".equals(jgid)) {
                //裁剪数量
                cell = row.createCell(++count);
                cell.setCellValue(store.get(i).getCaijianshu());
                cell.setCellStyle(center);
            }
            //入库数量
            cell = row.createCell(++count);
            cell.setCellValue(store.get(i).getRukushu());
            cell.setCellStyle(center);
            //出库数量
            cell = row.createCell(++count);
            cell.setCellValue(store.get(i).getChukushu());
            cell.setCellStyle(center);
            //出库数量
            cell = row.createCell(++count);
            cell.setCellValue(store.get(i).getShuliang());
            cell.setCellStyle(center);
            //品牌
            cell = row.createCell(++count);
            cell.setCellValue(store.get(i).getPinpai());
            cell.setCellStyle(center);
            //年份
            cell = row.createCell(++count);
            cell.setCellValue(store.get(i).getYear());
            cell.setCellStyle(center);
            //季节
            cell = row.createCell(++count);
            cell.setCellValue(store.get(i).getSeason());
            cell.setCellStyle(center);
        }
        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(count - 2, 5000);
        sheet.setColumnWidth((count - 1), 4000);
        return wb;
    }


}
